<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">搜索信息</label>
                            <div class="layui-input-inline">
                                <input type="text" name="searchVal" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableFilter" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="reset">重置密码</a>
        </script>
        <script type="text/html" id="statusTemp">
            {{#  if(d.status=='1'){ }}
            <input type="checkbox" checked lay-skin="switch" lay-filter="status" lay-event="switch" lay-text="正常|禁用">
            {{#  } else { }}
            <input type="checkbox"  lay-skin="switch" lay-filter="status" lay-event="switch" lay-text="正常|禁用">
            {{#  } }}
        </script>

    </div>
</div>
<div id="addcustomerdiv" style="display: none">

    <form class="layui-form" action="" style="position: absolute;top: 30px">
        <div class="layui-form-item">
            <label class="layui-form-label">登录名</label>
            <div class="layui-input-inline" style="width: 410px">
                <input type="text" name="loginName" required  lay-verify="required" placeholder="请输入登录名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-inline" style="width: 410px">
                <input type="text" name="email" required  lay-verify="required" placeholder="请输入邮箱" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">电话</label>
            <div class="layui-input-inline" style="width: 410px">
                <input type="text" name="userPhone" required  lay-verify="required" placeholder="请输入电话" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">选择框</label>
            <div class="layui-input-block" style="width: 410px">
                <select name="sex" lay-verify="required">
                    <option value=""></option>
                    <option value="0">女</option>
                    <option value="1">男</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="addcustomerformbts">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<div id="editcustomerdiv" style="display: none">

    <form class="layui-form" action="" style="position: absolute;top: 30px" lay-filter="editform">
        <div class="layui-hide">
            <input type="text" name="userId" required  lay-verify="required" autocomplete="off" class="layui-input">
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">登录名</label>
            <div class="layui-input-inline" style="width: 410px">
                <input type="text" name="loginName" required  lay-verify="required" placeholder="请输入登录名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-inline" style="width: 410px">
                <input type="text" name="email" required  lay-verify="required" placeholder="请输入邮箱" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">电话</label>
            <div class="layui-input-inline" style="width: 410px">
                <input type="text" name="userPhone" required  lay-verify="required" placeholder="请输入电话" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">选择框</label>
            <div class="layui-input-block" style="width: 410px">
                <select name="sex" lay-verify="required">
                    <option value=""></option>
                    <option value="0">女</option>
                    <option value="1">男</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="editcustomerformbts">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script th:inline="none">
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layer = layui.layer;


        //表格渲染
        table.render({
            elem: '#currentTableFilter',
            url: '/system/customer/list',
            method:'post',
            toolbar: '#toolbarDemo',
            id:"currentTableFilter",
            cols: [[
                {field: 'userId',type: "checkbox", width: 50},
                {field: 'loginName', minWidth: 50, title: '登录名',align:"center"},
                {field: 'email', minWidth: 50, title: '邮箱',align:"center"},
                {field: 'userPhone', minWidth: 50, title: '电话',align:"center"},
                {field: 'sex', minWidth: 50,templet:function(d){if(d.sex=='1'){return '男'} else{return '女'}}, title: '性别',align:"center"},
                {field: 'loginDate', minWidth: 50, title: '最后登录时间',align:"center"},
                {title: '状态', minWidth: 50, toolbar: '#statusTemp', align: "center"},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line',
            done:function(){
                tableData = table.cache.currentTableFilter;
            }
        });
        //状态更新
        form.on('switch(status)', function(obj){
            var index  = obj.othis.parents('tr').attr("data-index");
            var id = tableData[index].userId;
            //方法二取数据 （根据索引table.cache里面的行数据）
            if(obj.elem.checked)
            {
                $.ajax({
                    url:"/system/customer/edit",
                    method:"post",
                    data:{
                        userId:id,
                        status:1
                    },
                    success:function (res) {
                        if(res.code==0){
                            layer.msg("状态设置成功");
                            table.reload("currentTableFilter")
                        }
                        else{
                            layer.msg(res.message);
                        }
                    }
                });
            }
            else
            {
                $.ajax({
                    url:"/system/customer/edit",
                    method:"post",
                    data:{
                        userId:id,
                        status:2
                    },
                    success:function (res) {
                        if(res.code==0){
                            layer.msg("状态设置成功");
                            table.reload("currentTableFilter")
                        }
                        else{
                            layer.msg(res.message);

                        }
                    }
                });
            }
        });
        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            data = data.field;

            //执行搜索重载
            table.reload('currentTableFilter', {
                url: '/system/customer/search?searchVal='+data.searchVal
            });
            return false;
        })
        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加用户',
                    type: 1,
                    area: ['600px', '400px'],
                    content: $('#addcustomerdiv') ,
                    cancel: function(index, layero){
                        table.reload("currentTableFilter")
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            }
        });
        form.on('submit(addcustomerformbts)', function(data){

            $.ajax({
                type:'post',
                data:data.field,
                dataType:'json',
                url:'/system/customer/insert',
                success:function (data) {
                    console.log(data)
                    if(data.code=="0"){
                        layer.msg("添加成功")
                    }
                    else {
                        layer.msg("用户名重复")
                    }
                }
            })
            return false
        });
        form.on('submit(editcustomerformbts)', function(data){
            $.ajax({
                type:'post',
                data:data.field,
                dataType:'json',
                url:'/system/customer/edit',
                success:function (data) {
                    layer.msg(data.msg)
                }
            })
            return false
        });


        table.on('tool(currentTableFilter)', function (obj) {

            var data = obj.data;
            if(data.sex==null)
            {
                data.sex=0
            }
            form.val("editform",{
                userId:data.userId,
                loginName:data.loginName,
                email:data.email,
                userPhone:data.userPhone,
                sex:data.sex
            })
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '编辑用户',
                    type: 1,
                    area: ['600px', '400px'],
                    content: $('#editcustomerdiv'),
                    cancel: function(index, layero){
                        table.reload("currentTableFilter")
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            }
            else if (obj.event === 'delete') {
                layer.confirm('真的要删除吗', function (index) {
                    $.ajax({
                        url:"/system/customer/remove/"+data.userId,
                        method:"post",
                        success:function (res) {
                            if(res.code==0){
                                layer.msg(res.msg)
                                table.reload("currentTableFilter")
                            }
                            else{
                                layer.msg(res.msg)
                                table.reload("currentTableFilter")
                            }
                        },
                        error:function (res) {
                            layer.msg(res.message)
                        }
                    });
                    layer.close(index);
                });
            }
            else if(obj.event === 'reset'){
                layer.confirm("确定要重置改用户的密码吗？",function (index) {
                    $.ajax({
                        url:"/system/customer/resetPwd/"+data.userId,
                        method:"post",
                        success:function (res) {
                            if(res.code==0){
                                layer.msg(res.msg)
                                table.reload("currentTableFilter")
                            }
                            else{
                                layer.msg(res.msg)
                                table.reload("currentTableFilter")
                            }
                        },
                        error:function (res) {
                            layer.msg(res.msg)
                        }
                    });
                    layer.close(index);
                })
                return false;
            }
        });

    });
</script>

</body>
</html>
